package cn.itcast.service.impl;

import cn.itcast.dataobject.Account;
import cn.itcast.repository.AccountMapper;
import cn.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author: wangxg
 * @date: 2019-06-19 21:36
 **/

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired(required = false)
    private AccountMapper accountMapper;


    public List<Account> findAll() {
        return accountMapper.selectAll();
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(Account account) {
        accountMapper.insert(account);
    }

    public Account findByName(String name) {
        // select id, name, mmoney from account were 1=1;
        Example example = new Example(Account.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        return accountMapper.selectOneByExample(example);
    }

    public List<Account> findAccountLikeName(Integer num) {
        Example example = new Example(Account.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThan("id", num);
        return accountMapper.selectByExample(example);
    }
}
